<?php
if (!defined('ABSPATH')) { exit; }

function scf_daily_almanac_shortcode($atts) {
    $ts = current_time('timestamp');
    $date_str = date_i18n('Y-m-d', $ts);
    $weekday_idx = (int) date_i18n('w', $ts); // 0-6, 周日=0
    $weekday_map = ['周日','周一','周二','周三','周四','周五','周六'];

    // 简易“宜/忌”占位数据（演示用途）
    $yi_map = [
        ['祭祀','沐浴','理发','扫舍'],
        ['纳财','开市','开工','求学'],
        ['出行','会友','谈判','签约'],
        ['装修','动土','安床','作灶'],
        ['祈福','拜访','求医','求职'],
        ['婚嫁','订盟','纳采','移徙'],
        ['探亲','旅行','买房','购置']
    ];
    $ji_map = [
        ['诉讼','远行','搬迁','破土'],
        ['借贷','诉讼','探病','远行'],
        ['酗酒','赌博','破财','争执'],
        ['开市','签约','搬家','远行'],
        ['开业','动土','婚嫁','远行'],
        ['开工','破土','动土','签约'],
        ['开市','动土','签约','远行']
    ];

    $yi = $yi_map[$weekday_idx];
    $ji = $ji_map[$weekday_idx];

    $atts = shortcode_atts(array(
        'date' => '', // 可选：指定日期，格式 YYYY-MM-DD；默认今日
    ), $atts, 'daily_almanac');

    $date_str = '';
    if (!empty($atts['date'])) {
        $date_str = preg_replace('/[^0-9\-]/', '', $atts['date']);
    } else {
        $date_str = date('Y-m-d', current_time('timestamp'));
    }

    // 按需加载前端脚本
    wp_enqueue_script('scf-almanac');

    ob_start();
    ?>
    <div class="scf-module scf-almanac" data-date="<?php echo esc_attr($date_str); ?>">
        <div class="scf-almanac-header">
            <div class="scf-almanac-date">加载日期中……</div>
            <div class="scf-almanac-lunar">加载农历中……</div>
        </div>
        <div class="scf-almanac-body">
            <div class="scf-almanac-section">
                <h4>宜</h4>
                <ul class="scf-list scf-almanac-yis"><li>加载中……</li></ul>
            </div>
            <div class="scf-almanac-section">
                <h4>忌</h4>
                <ul class="scf-list scf-almanac-jis"><li>加载中……</li></ul>
            </div>
        </div>
    </div>
    <?php
    return ob_get_clean();
}